package lec10.am;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

import javax.swing.JFrame;

public class DrawPolyShape extends JFrame implements MouseListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Vector<Point> points = new Vector<>();

	@Override
	public void paint(Graphics g) {
		g.setColor(Color.gray);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());

	}

	public DrawPolyShape() {
		super("My App");
		setSize(400, 400);
		this.addMouseListener(this);
		setVisible(true);
	}

	public static void main(String[] args) {
		new DrawPolyShape();

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		Graphics g = this.getGraphics();
		if (e.getButton() == 1)// leftButton
		{
			g.drawArc(e.getX()-5, e.getY()-5, 10, 10, 0, 360);
			points.add(new Point(e.getX(), e.getY()));
		} else if (e.getButton() == 2)// middleButton
		{
			g.setColor(Color.gray);
			g.fillRect(0, 0, this.getWidth(), this.getHeight());
			g.setColor(Color.red);
		} else if (e.getButton() ==3)// rightButton
		{
			for (int i = 0; i < points.size(); i++) {

				Point from = points.get(i % points.size());
				Point to = points.get((i + 1) % points.size());
				g.drawLine(from.x, from.y, to.x, to.y);

			}
			points.clear();

		}

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

}
